<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>员工管理</title>
<#include "../common/link.ftl">

</head>
<body class="hold-transition skin-black sidebar-mini">
<div class="wrapper">
<#include "../common/navbar.ftl">
    <!--菜单回显-->
<#assign currentMenu="employee"/>
<#include "../common/menu.ftl">
    <div class="content-wrapper">
        <section class="content-header">
            <h1>员工管理</h1>
        </section>
        <section class="content">
            <div class="box">
                <!--高级查询--->
                <div style="margin: 10px;">
                    <form class="form-inline" id="searchForm" action="/employee/list" method="post">
                        <input type="hidden" name="currentPage" id="currentPage" value="1">
                        <div class="form-group">
                            <label for="keyword">关键字:</label>
                            <#--<input type="text" class="form-control" name="keyword" placeholder="请输入姓名/邮箱"
                                   value="${qo.keyword}">-->
                        </div>
                        <div class="form-group">
                            <label for="dept"> 部门:</label>
                            <select class="form-control" id="dept" name="deptId" data-live-search="true">
                                <option value="">全部</option>
                            <#list departments as d>
                                <option value="${d.id}">${d.name}</option>
                            </#list>
                            </select>
                            <#--<script>
                                $("#dept").val(${qo.deptId})
                            </script>&ndash;&gt;-->
                        </div>
                        <button type="submit" class="btn btn-primary">
                            <span class="glyphicon glyphicon-search"></span> 查询
                        </button>
                        <a href="/employee/input" class="btn btn-success btn-input">
                            <span class="glyphicon glyphicon-plus"></span> 添加
                        </a>
                        <a href="/employee/exportXls" class="btn btn-warning">
                            <span class="glyphicon glyphicon-download"></span> 导出
                        </a>
                        <a href="#" class="btn btn-warning btn-import">
                            <span class="glyphicon glyphicon-upload"></span> 导入
                        </a>
                        <a href="#" class="btn btn-danger btn-batchDelete">
                            <span class="glyphicon glyphicon-remove"></span> 批量删除
                        </a>
                    </form>
                </div>
                <div class="box-body table-responsive ">
                    <table class="table table-hover table-bordered table-striped">
                        <thead>
                        <tr>
                            <th><input type="checkbox" onchange="checkChange(this)" id="allCb"></th>
                            <th>编号</th>
                            <th>用户名</th>
                            <th>真实姓名</th>
                            <th>密码</th>
                            <th>邮箱</th>
                            <th>年龄</th>
                            <th>是否管理员</th>
                            <th>部门</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>

                        <#list page.records as e>
                        <tr>
                            <td><input type="checkbox" class="cb" name="ids" value="${e.id}"></td>
                            <td>${e_index + 1}</td>
                            <td>${e.username!}</td>
                            <td>${e.name!}</td>
                            <td>${e.password!}</td>
                            <td>${e.email!}</td>
                            <td>${e.age!}</td>
                            <td><#if (e.admin!)>是<#else>否</#if></td>
                            <td>${e.deptId!}</td>
                            <#--<td>${e.statusName!}</td>-->
                            <td>
                                <a href="/employee/input?id=${e.id}" class="btn btn-info btn-xs btn_redirect">
                                    <span class="glyphicon glyphicon-pencil"></span> 编辑
                                </a>
                                <a href="#" class="btn btn-danger btn-xs btn-delete"
                                   data-url="/employee/delete?id=${e.id}">
                                    <span class="glyphicon glyphicon-trash"></span> 删除
                                </a>

                                <a href="#" data-json='${e.json}' data-name="${e.name}"
                                   class="btn  btn-warning btn-xs btn_status">
                                    <span class="glyphicon glyphicon-tags"></span>禁用
                                </a>
                            </td>
                        </tr>
                        </#list>
                        </tbody>
                    </table>
                    <!--分页-->
                <#include "../common/page.ftl">
                </div>

            </div>
        </section>
    </div>
<#include "../common/footer.ftl">
</div>
<div class="modal fade" id="importModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">导入</h4>
            </div>
            <form class="form-horizontal" action="/employee/importXls" enctype="multipart/form-data" method="post"
                  id="importForm1">
                <div class="modal-body">
                    <div class="form-group" style="margin-top: 10px;">
                        <label for="name" class="col-sm-3 control-label"></label>
                        <div class="col-sm-6">
                            <!-- 文件上传框 -->
                            <input type="file" accept="application/vnd.ms-excel" name="file">
                        </div>
                    </div>
                    <div class="form-group" style="margin-top: 10px;">
                        <div class="col-sm-3"></div>
                        <div class="col-sm-6">
                            <a href="xls/employee_import.xls" class="btn btn-success">
                                <span class="glyphicon glyphicon-download"></span> 下载模板
                            </a>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-primary btn-submit">保存</button>
                </div>
            </form>
        </div>
    </div>
</div>
<script>
    //全选 / 反选
    function checkChange(domEle) {
        let flag = $(domEle).prop('checked'); // 全选
        $('input[name=ids]').prop('checked', flag); // 反选
    }

    //复选框
    $('input[name=ids]').click(function () {
        var total = true;
        $('input[name=ids]').each(function (index, domEle) {
            total = total && $(domEle).prop('checked');
        });
        $('#allCb').prop('checked', total);
    });

    //批量删除
    $('.btn-batchDelete').click(function () {
        var ids = [];
        $('input[name=ids]').each(function (index, domEle) {
            if ($(domEle).prop('checked')) {
                ids.push($(domEle).val());
            }
        });
        jQuery.ajaxSettings.traditional = true;
        console.log(ids);
        $.post('/employee/batchDelete.do', {ids: ids}, function (data) {
            if (data.success) {
                Swal.fire({
                    title: '确定删除吗?',
                    icon: 'success',
                    confirmButtonColor: '#3085d6',
                    cancelButtonColor: '#d33',
                    confirmButtonText: 'OK'
                }).then((result) => {
                    if (result.value) {
                        window.location.href = "/employee/list";
                    }
                })
            } else {
                Swal.fire(
                        data.msg,
                        '',
                        'error'
                )
            }
        });
    });

    $(".btn-import").click(function () {
        $("#importModal").modal('show');
    });

    $(".btn-submit").click(function () {
        //提交导入表单
        $("#importForm1").ajaxForm(function (data) {
            if (data.success) {
                Swal.fire({
                    title: '导入成功',
                    icon: 'success',
                    confirmButtonText: 'OK'
                }).then((result) => {
                    if (result.value) {
                        window.location.reload();//刷新
                    }
                })
            } else {
                Swal.fire(
                        data.msg,
                        '',
                        'error'
                )
            }
        });
    })

    /* var $status =  $('.btn-status');
     $status.click(function () {
         var status = $(this).data('status');
         var id = $(this).data('id');
         $.post("/employee/changeStatus",{status:status,id:id},function (data) {
             window.location.reload();
         })
     })*/
</script>
<script>
    // 禁用/恢复功能
    $('.btn_status').click(function () {
        let data = $(this).data('json');
        data.status = !data.status;
        $.post('/employee/changeStatus.do', data, function (data) {
            if (data.success) {
                Swal.fire({
                    title: '操作成功',
                    icon: 'success',
                    confirmButtonColor: '#3085d6',
                    cancelButtonColor: '#d33',
                    confirmButtonText: 'OK'
                }).then((result) => {
                    if (result.value) {
                        window.location.reload();
                    }
                })
            } else {
                Swal.fire(
                        data.msg,
                        '',
                        'error'
                )
            }
        });
    });

</script>
</body>
</html>
